<?php
/**
* @package mosRest
* @author Chad Auld and Ozgur Cem Sen (code@brilaps.com)
* @copyright Brilaps, LLC (http://brilaps.com)
* @link http://brilaps.com || http://wiki.brilaps.com
* @license http://www.opensource.org/licenses/gpl-license.php GNU/GPL v.2.
*/

/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

//Fix to allow compatibility with J!
if (!function_exists( 'T_' )) { function T_($string) {return $string;} }

class HTML_users {

	function showUsers( &$rows, $pageNav, $search, $option ) {
		?>
		<form action="index2.php" method="post" id="adminForm" name="adminForm">
		<table class="adminheading">
		<tr>
			<th class="user">
			<?php echo T_('RESTful API Manager'); ?>
			</th>
			<td>
			<?php echo T_('Filter:'); ?>
			</td>
			<td>
			<input type="text" name="search" value="<?php echo $search;?>" class="inputbox" onchange="document.adminForm.submit();" />
			</td>
		</tr>
		</table>
		<table class="adminlist">
		<tr>
			<th width="2%" class="title">
			#
			</th>
			<th width="3%" class="title">
			<input type="checkbox" name="toggle" value="" onclick="YAHOO.miacms.base.checkAll(<?php echo count($rows); ?>);" />
			</th>
			<th class="title">
			<?php echo T_('Developer Name'); ?>
			</th>
			<th class="title" nowrap="nowrap">
			<?php echo T_('Product Name'); ?>
			</th>
			<th class="title">
			<?php echo T_('Contact E-mail'); ?>
			</th>
		</tr>
		<?php
		$k = 0;
		for ($i=0, $n=count( $rows ); $i < $n; $i++) {
			$row  =& $rows[$i];
			$link = htmlentities('index2.php?option=com_rest&task=editA&id='. $row->id. '&hidemainmenu=1');
			?>
			<tr class="<?php echo "row$k"; ?>">
				<td>
				<?php echo $i+1+$pageNav->limitstart;?>
				</td>
				<td>
				<?php echo mosHTML::idBox( $i, $row->id ); ?>
				</td>
				<td>
				<a href="<?php echo $link; ?>">
				<?php echo $row->developer_name; ?>
				</a>
				</td>
				<td>
				 <?php echo $row->product_name; ?>
				</td>
				<td>
				<a href="mailto:<?php echo $row->contact_email; ?>">
				<?php echo $row->contact_email; ?>
				</a>
				</td>
			</tr>
			<?php
			$k = 1 - $k;
		}
		?>
		</table>
		<?php echo $pageNav->getListFooter(); ?>
		<input type="hidden" name="option" value="<?php echo $option;?>" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" id="boxchecked" name="boxchecked" value="0" />
		<input type="hidden" name="hidemainmenu" value="0" />
		</form>
		<?php
	}

	function edituser( $row, $lists, $option ) {
		?>
		<form action="index2.php" method="post" id="adminForm" name="adminForm">
		<table class="adminheading">
		<tr>
			<th class="user">
			<?php echo T_('REST User:'); ?> <small><?php echo T_('Edit');?></small>
			</th>
		</tr>
		</table>
		<table width="100%">
		<tr>
			<td width="60%" valign="top">
				<table class="adminform">
				<tr>
					<th colspan="2">
					<?php echo T_('REST User Details'); ?>
					</th>
				</tr>
				<tr>
					<td width="100">
					<?php echo T_('Developer Name:'); ?>
					</td>
					<td width="85%">
					<?php echo $row->developer_name; ?>
					</td>
				</tr>
				<tr>
					<td>
					<?php echo T_('Contact E-mail:'); ?>
					</td>
					<td>
					<?php echo $row->contact_email; ?>
					</td>
				</tr>
				<tr>
					<td>
					<?php echo T_('Product Name:'); ?>
					</td>
					<td>
					<?php echo $row->product_name; ?>
					</td>
				</tr>
				<tr>
					<td>
					<?php echo T_('Web Application URL:'); ?>
					</td>
					<td>
					<?php echo $row->web_app_url; ?>
					</td>
				</tr>
				<tr>
					<td>
					<?php echo T_('Description:'); ?>
					</td>
					<td>
					<?php echo $row->description; ?>
					</td>
				</tr>
				<tr>
					<td>
					<?php echo T_('Registration Date:'); ?>
					</td>
					<td>
					<?php echo $row->registration_date; ?>
					</td>
				</tr>
				<tr>
					<?php
					if (empty($row->approval_date) || ($row->approval_date=='0000-00-00 00:00:00')) {
						?>
						<td>
						<?php echo T_('Enable API Access:'); ?>
						</td>
						<td>
						<?php echo $lists['approve']; ?>
						</td>
						<?php
					} else {
						?>
						<td>
						<?php echo T_('Approval Date'); ?>
						</td>
						<td>
						<?php echo $row->approval_date; ?>
						</td>
						<?php
							if (!empty($row->rest_key)) {
								//Only display the block function when the user has already been approved and activated
								?>
								<tr>
									<td>
									<?php echo T_('Block User'); ?>
									</td>
									<td>
									<?php echo $lists['block']; ?>
									</td>
								</tr>
								<?php
							}
					}
					?>	
				</tr>
				<tr>
					<td>
					<?php echo T_('API Key:'); ?>
					</td>
					<td>
					<?php 
						if (empty($row->rest_key)) {
							echo T_('An API key will be generated after access has been approved and the user has activated the account.'); 
						} else {
							echo $row->rest_key;
						}
					?>
					</td>
				</tr>
				</table>
			</td>
		</tr>
		</table>
		<input type="hidden" name="id" value="<?php echo $row->id; ?>" />
		<input type="hidden" name="option" value="<?php echo $option; ?>" />
		<input type="hidden" name="task" value="" />
		</form>
		<?php
	}
	
	/**
	 * @param option
	 * @return builds admin configuration options
	 */
	function showConfig( $option ) {
		global $mosConfig_live_site, $mosConfig_absolute_path;
		$config_file = $mosConfig_absolute_path.'/administrator/components/com_rest/config.rest.php';
		if (!file_exists($config_file) || !is_writable($config_file)) {
			echo '<p class="warning">'.T_('WARNING: The REST API is either missing or not writable!').'</p>';
		} else {
		    //Include base REST settings
		    $allow_rest_api_access = '';
		    $log_api_requests = '';
		    $allow_rest_registration = '';
		    $show_rest_registration_disclaimer = '';
		    $rest_registration_disclaimer_message = '';
		    include($mosConfig_absolute_path.'/administrator/components/com_rest/config.rest.php');
		}
		?>
		<script type="text/javascript">
			YAHOO.miacms.base.submitbutton = function(pressbutton) {
			    var form = YAHOO.util.Dom.get("adminForm") || document.adminForm;
    			if (pressbutton === 'cancel') {
    				YAHOO.miacms.base.submitform( pressbutton );
    				return;
    			}
    			YAHOO.miacms.base.submitform( 'savesettings' );
			};
		</script>
	  <form action="index2.php" method="post" id="adminForm" name="adminForm">
		<table width="70%" class="adminForm">
			<tr>
				<td><?php echo T_('Allow the REST API:'); ?></td>
				<td><?php echo mosHTML::yesnoRadioList( 'allow_rest_api_access', 'class="inputbox"', $allow_rest_api_access );
					$tip = T_('If yes, allows external access to the site through the RESTful interface');
					echo mosToolTip($tip);
				?></td>
			</tr>
			<tr>
				<td><?php echo T_('Log API Requests:'); ?></td>
				<td><?php echo mosHTML::yesnoRadioList( 'log_api_requests', 'class="inputbox"', $log_api_requests );
					$tip = T_('If yes, All API requests are tracked in the database.  This could eventually slow down the API if not monitored.');
					echo mosToolTip($tip);
				?></td>
			</tr>
			<tr>
				<td><?php echo T_('Allow New REST API Registrations:'); ?></td>
				<td><?php echo mosHTML::yesnoRadioList( 'allow_rest_registration', 'class="inputbox"', $allow_rest_registration );
					$tip = T_('If yes, allows users to self-register');
					echo mosToolTip($tip);
				?></td>
			</tr>
			<tr>
				<td><?php echo T_('Enable Registration Disclaimer and Privacy Policy:'); ?></td>
				<td><?php echo mosHTML::yesnoRadioList( 'show_rest_registration_disclaimer', 'class="inputbox"', $show_rest_registration_disclaimer );
				$tip = T_('Show API Registration Disclaimer and Privacy Policy on the registration page');
					echo mosToolTip($tip); 
				?>
				</td>
			</tr>
			<tr>
				<td><?php echo T_('Registration Disclaimer and Privacy Policy Message:'); ?></td>
				<td><textarea class="text_area" cols="60" rows="2" style="width:400px; height:40px" name="rest_registration_disclaimer_message"><?php echo htmlspecialchars($rest_registration_disclaimer_message, ENT_QUOTES); ?></textarea><?php
					$tip = T_('A message that displays on your registration page if the Registration Disclaimer and Privacy Policy is enabled');
					echo mosToolTip($tip);
				?>
				</td>
			</tr>
		</table>
	  <input type="hidden" name="option" value="<?php echo $option; ?>" />
	  <input type="hidden" name="task" value="" />
	  <input type="hidden" id="boxchecked" name="boxchecked" value="0" />
		</form>
		<?php
	}
	
}
?>